package com.houdoudou.affair.jsp;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houdoudou.bean.BeanParseException;
import com.houdoudou.bean.Financer;
import com.houdoudou.bean.Programmer;
import com.houdoudou.dao.ConnFactory;
import com.houdoudou.dao.DatabaseAndTableDao;
import com.houdoudou.dao.FinancerDao;
import com.houdoudou.dao.ProgrammerDao;

public class SuperProgrammerAffairJsp {

/*------------------------------------------新增程序员-------------------------------------------------*/	
	
	public static void createProgrammer(HttpServletRequest req, HttpServletResponse resp){		
	    Session se=null;
		boolean success=false;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------
		
		Programmer programmer= new Programmer();
		programmer.setSecret(req.getParameter("secret"));
		programmer.setTelnum(req.getParameter("telnum"));
		programmer.setType(Integer.parseInt(req.getParameter("type")));
		try {
			Programmer.checkValid(programmer);
		} catch (Exception e2) {
			e2.printStackTrace();
			Common.forwardToJsp(req,resp,"/programmer/createProgrammer.jsp","传入参数有误"+e2.getMessage());
		}
		programmer= Programmer.encrypt(programmer);
		System.out.println(""+programmer.getSecret()+programmer.getTelnum());
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   ProgrammerDao programmerDao= new  ProgrammerDao(conn);
		   Programmer programmerExist= programmerDao.selectByTelnum(programmer.getTelnum());
		   if(!programmerExist.getTelnum().isEmpty()){
			  success=false;
			   errorInfo="该用户已经存在";
		   }else{
			   programmerDao.create(programmer); 
			   success=true;
			   errorInfo="创建成功";
		   }
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {	conn.rollback();	} catch (SQLException e1) {	e1.printStackTrace();}
			 success=false;
			  errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
			
			 //处理事务结果跳转------------------
			 String jumpJspPath=req.getContextPath()+"/programmer/createProgrammer.jsp";
				Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
			 
			 
	}
	
	
/*------------------------------------------搜索程序员-------------------------------------------------*/	
	
	public static void searchProgrammer(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------		
		Programmer programmer= new Programmer();	
		programmer.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		 List<Programmer> programmers = new ArrayList<Programmer>();
		System.out.println(""+programmer.getSecret()+programmer.getTelnum());
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   ProgrammerDao programmerDao= new  ProgrammerDao(conn);
		   
		   if(programmer.getTelnum().isEmpty()){
			programmers= programmerDao.selectAll();
			success=true;
			   errorInfo=null;
		   }else{
			  Programmer temp= programmerDao.selectByTelnum(programmer.getTelnum());
			  programmers.add(temp);
			  success=true;
			   errorInfo=null;
		   }
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
			
				 req.setAttribute("success", true);	
				 req.setAttribute("errorInfo", errorInfo);		
			      if(success){
			    	  req.setAttribute("programmers", programmers);	 
			      }
			 //处理事务结果跳转------------------
			      String jumpJspPath="/programmer/manageProgrammer.jsp";
					Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
			 
			 
	}
	
/*------------------------------------------删除程序员-------------------------------------------------*/	
	
	public static void deleteProgrammer(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------		
		Programmer programmer= new Programmer();	
		programmer.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		
		System.out.println(""+programmer.getSecret()+programmer.getTelnum());
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   ProgrammerDao programmerDao= new  ProgrammerDao(conn);
		   programmerDao.delete(programmer.getTelnum());
		   success=true;
		   errorInfo="删除成功,已刷新界面，请重新搜索一遍";
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------	
			 //处理事务结果跳转------------------
				 String jumpJspPath=req.getContextPath()+"/programmer/manageProgrammer.jsp";
				 Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
			 
	}
	
/*------------------------------------------程序员登陆-------------------------------------------------*/	
	
	public static void programmerLogin(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		 Programmer programmerExist= new Programmer();
		//获取Bean---------------------------	
		Programmer programmer= new Programmer();
		programmer.setSecret(req.getParameter("secret"));
		programmer.setTelnum(req.getParameter("telnum"));
		programmer= Programmer.encrypt(programmer);
		System.out.println(""+programmer.getSecret()+programmer.getTelnum());
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   ProgrammerDao programmerDao= new  ProgrammerDao(conn);
		   programmerExist= programmerDao.selectByTelnum(programmer.getTelnum());
		   if(programmerExist.getTelnum().isEmpty()){
			  success=false;
			   errorInfo="用户不存在";
		   }else{
			   if(programmerExist.getSecret().equals(programmer.getSecret())){
				   success=true;
				   errorInfo="登陆成功";
			   }else{
				   success=false;
				   errorInfo="密码不正确";
			   }
		   }
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			   success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
		 String jumpJspPath="";
			 if(success){				 
				 req.getSession().setAttribute("programmerTelnum", programmerExist.getTelnum());
				 req.getSession().setAttribute("programmerSecret", programmerExist.getSecret());
				 req.getSession().setAttribute("programmerType", programmerExist.getType());
			
					if(programmerExist.getType()==1){
						jumpJspPath=req.getContextPath()+"/programmer/superProgrammer.jsp";
						Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
					}
                   if(programmerExist.getType()==2){
                	   jumpJspPath=req.getContextPath()+"/normalProgrammer/normalProgrammer.jsp";
                	   Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
					}
			       }else{		
			    	   req.setAttribute("errorInfo", errorInfo);
			    	   req.setAttribute("result", 0);	
			    	   jumpJspPath= "/programmer/programmerLogin.jsp";
			    	   Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
			       }

   
	}
	
/*------------------------------------------修改程序员-------------------------------------------------*/	
	
	public static void modifyProgrammer(HttpServletRequest req, HttpServletResponse resp){		
		boolean success=true;
		String  errorInfo="未执行任何操作";
		 String jumpJspPath="/normalProgrammer/modifyProgrammer.jsp";	
		//获取Bean---------------------------	
		Programmer programmer = null;
		try {
			programmer = Programmer.beanFromRequest(req);
			if(programmer.getTelnum().length()!=11) {throw new BeanParseException("电话位数不对");}
		       } catch (Exception e2) {
			       e2.printStackTrace();
			       req.setAttribute("programmer", programmer);
			       Common.forwardToJsp(req,resp,jumpJspPath,"输入数据有误"+e2.getMessage());
			       return;
		         }
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();	  
		   ProgrammerDao programmerDao= new  ProgrammerDao(conn);
		   programmer=  programmerDao.selectByTelnum(programmer.getTelnum());
		   req.setAttribute("programmer", programmer);		      
		   Common.forwardToJsp(req,resp,jumpJspPath,null);
		}catch(Exception e){
			e.printStackTrace();
			   req.setAttribute("programmer", programmer);	
			   Common.redirectToJsp(req,resp,req.getContextPath()+jumpJspPath,"数据库异常");
			   return;
		}finally{
			 ConnFactory.closeConn(conn);	
		}	
	//处理事务结果，赋值----------------------
		
   
	}
/*------------------------------------------更新程序员-------------------------------------------------*/	
	
	public static void updateProgrammer(HttpServletRequest req, HttpServletResponse resp){		
		boolean success=true;
		 String jumpJspPath="/normalProgrammer/modifyProgrammer.jsp";
		//获取Bean---------------------------	
		Programmer programmer = null;	
		try{
		programmer = Programmer.beanFromRequest(req);		
		}catch(Exception e ){
			e.printStackTrace();
			req.setAttribute("programmer", programmer);
			 Common.forwardToJsp(req,resp,jumpJspPath,"传入参数不对");
			 return;
		}
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   ProgrammerDao programmerDao= new  ProgrammerDao(conn);
		   Programmer programmerExist= programmerDao.selectByTelnum(programmer.getTelnum());
		   programmerExist= Programmer.decrypt(programmerExist);
		   programmer= Programmer.merge(programmerExist, programmer);
		   try{
		   Programmer.checkValid(programmer);
		   programmer= Programmer.encrypt(programmer);
		   }catch(Exception e){
			   e.printStackTrace();
			   req.setAttribute("programmer", programmer);
			   Common.forwardToJsp(req,resp,jumpJspPath,"传入参数不对"+e.getMessage());
			   return;
		   }
		   int acount =programmerDao.update(programmer);
			 req.setAttribute("programmer", programmer);
			 Common.forwardToJsp(req,resp,jumpJspPath,"修改成功");
		 
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try { 	conn.rollback();} catch (SQLException e1) {e1.printStackTrace();}
			 
			   req.setAttribute("programmer", programmer);
			    Common.forwardToJsp(req,resp,jumpJspPath,"数据库异常");
		}finally{
			 ConnFactory.closeConn(conn);	
			
		}
		
   
	}
			
/*------------------------------------------新增财务人员-------------------------------------------------*/	
	
	public static void createFinancer(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=true;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------
		Financer financer;
		try{
		financer= Financer.beanFromRequest(req);
		financer= Financer.encrypt(financer);
		}catch(Exception e){
			e.printStackTrace();
			success=false;
			   errorInfo="传入参数有误";
			   String jumpJspPath=req.getContextPath()+"/programmer/createFinancer.jsp";
			   Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
			   return ;
		}
		
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		  FinancerDao financerDao= new FinancerDao(conn);
		   Financer financerExist= financerDao.selectByTelnum(financer.getTelnum());
		   if(!financerExist.getTelnum().isEmpty()){
			  success=false;
			   errorInfo="该用户已经存在";
		   }else{
			   financerDao.create(financer); 
			   success=true;
			   errorInfo="创建成功";
		   }
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
			
			 //处理事务结果跳转------------------
			 String jumpJspPath=req.getContextPath()+"/programmer/createProgrammer.jsp";
				Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
			 
			 
	}	
	
/*------------------------------------------搜索财务人员-------------------------------------------------*/	
	
	public static void searchFinancer(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------		
		Financer financer= new Financer();	
		financer.setTelnum(req.getParameter("telnum"));
		// 初始化搜索结果
		 List<Financer> financers = new ArrayList<Financer>();
	
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   FinancerDao financerDao= new  FinancerDao(conn);
		   
		   if(financer.getTelnum().isEmpty()){
			financers= financerDao.selectAll();
			success=true;
			   errorInfo=null;
		   }else{
			  Financer temp= financerDao.selectByTelnum(financer.getTelnum());
			  financers.add(temp);
			  success=true;
			   errorInfo=null;
		   }
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------
			
				 req.setAttribute("success", success);	
			      if(success){
			    	  req.setAttribute("financers", financers);	 
			      }
			 //处理事务结果跳转------------------
			      String jumpJspPath="/programmer/manageFinancer.jsp";
					Common.forwardToJsp(req,resp,jumpJspPath,errorInfo);
			 
			 
	}
	
/*------------------------------------------删除程序员-------------------------------------------------*/	
	
	public static void deleteFinancer(HttpServletRequest req, HttpServletResponse resp){		
		
		boolean success=false;
		String  errorInfo="未执行任何操作";
		//获取Bean---------------------------		
		Financer financer= new Financer();	
		financer.setTelnum(req.getParameter("telnum"));
		
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
		   conn= ConnFactory.getConn();
		   conn.setAutoCommit(false);
		   FinancerDao financerDao= new  FinancerDao(conn);
		   financerDao.delete(financer.getTelnum());
		   success=true;
		   errorInfo="删除成功,已刷新界面，请重新搜索一遍";
		   conn.commit();
		}catch(Exception e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			 success=false;
			   errorInfo="数据库异常";
		}finally{
			 ConnFactory.closeConn(conn);	
		}
		
	//处理事务结果，赋值----------------------	
			 //处理事务结果跳转------------------
				 String jumpJspPath=req.getContextPath()+"/programmer/manageFinancer.jsp";
				 Common.redirectToJsp(req,resp,jumpJspPath,errorInfo);
			 
	}
		

	

}
